package com.jlnk.androidfmc.entity;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Date;
import java.util.List;

public class Order implements Parcelable {
    private String orderId;       // 订单号
    private Date orderDate;       // 订单日期
    private double totalAmount;    // 订单总金额
    private List<SelectedItem> items; // 存储选中的商品项列表

    // 构造方法
    public Order() {
        // 可以添加一些初始化代码
    }

    // Parcelable构造方法
    protected Order(Parcel in) {
        orderId = in.readString();
        long tmpOrderDate = in.readLong();
        orderDate = tmpOrderDate != -1 ? new Date(tmpOrderDate) : null;
        items = in.createTypedArrayList(SelectedItem.CREATOR); // 使用 SelectedItem 的 CREATOR // 这里应该是 SelectedItem.CREATOR
        totalAmount = in.readDouble();
    }

    // 订单号
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    // 订单日期
    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    // 订单商品项

    // 在Order类中
    public void setItems(List<SelectedItem> items) {
        this.items = items;
    }

    // 订单总金额
    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    // Parcelable 接口实现
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(orderId);
        dest.writeLong(orderDate != null ? orderDate.getTime() : -1);
        dest.writeTypedList(items); // 现在 items 是 List<SelectedItem>，且 SelectedItem 实现了 Parcelable
        dest.writeDouble(totalAmount);
    }

// 保持 CREATOR 实现不变，因为 SelectedItem 的 CREATOR 已经在 SelectedItem 类中定义

    // Parcelable.Creator 实现
    public static final Parcelable.Creator<Order> CREATOR =
            new Parcelable.Creator<Order>() {
                public Order createFromParcel(Parcel in) {
                    return new Order(in);
                }
                public Order[] newArray(int size) {
                    return new Order[size];
                }
            };

    public List<SelectedItem> getItems() {
        return items;
    }
}
